package com.ruoyi.hr.controller.hr;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.domain.Companys;
import com.ruoyi.hr.domain.DepartmentsRegister;
import com.ruoyi.hr.service.*;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 部门管理控制层
 */
@Controller
@RequestMapping("/depart/admin")
public class DepartAdminController extends BaseController {

    @Autowired
    AuthorityService authorityService;

    @Autowired
    DepartAdminService departAdminService;

    @Autowired
    CompAdminService compAdminService;

    @Autowired
    CompanysService companysService;

    @Autowired
    JinPengService jinPengService;

    @Autowired
    DepartmentsService departmentsService;

    @Autowired
    ISysUserService iSysUserService;

    private String prefix = "hr/departAdmin";



    /**
     * 获取所有在岗人员姓名用逗号隔开
     *
     * @return
     */
    @GetMapping("/getEmpRow")
    @ResponseBody
    public AjaxResult getEmpRow() {
        List<String> nameList = jinPengService.getEmpRow();
        String[] strings = new String[nameList.size()];
        for (int i = 0; i < nameList.size(); i++) {
            strings[i] = nameList.get(i);
        }
        AjaxResult ajax = new AjaxResult();
        ajax.put("value", strings);
        return ajax;
    }


    /**
     * 获取所有生效岗位用逗号隔开
     *
     * @return
     */
    @GetMapping("/getDepRow")
    @ResponseBody
    public AjaxResult getDepRow() {
        List<String> nameList = departmentsService.getDepRow();
        String[] strings = new String[nameList.size()];
        for (int i = 0; i < nameList.size(); i++) {
            strings[i] = nameList.get(i);
        }
        AjaxResult ajax = new AjaxResult();
        ajax.put("value", strings);
        return ajax;
    }


    /**
     * 新增登记部门
     *
     * @param departmentsRegister
     * @return
     */
    @PostMapping("/add/register")
    @ResponseBody
    public AjaxResult addSaveRegister(@Validated DepartmentsRegister departmentsRegister) {
        String funcationName = departmentsRegister.getFuncationName();
        String directorName = departmentsRegister.getDirectorName();
        String director2Name = departmentsRegister.getDirector2Name();
        if (!"".equals(funcationName)) {
            departmentsRegister.setFuncationId(getArryZero(funcationName));
        }
        if (!"".equals(directorName)) {
            departmentsRegister.setDirector(getArryZero(directorName));
        }
        if (!"".equals(director2Name)) {
            departmentsRegister.setDirector2(getArryZero(director2Name));
        }
        departmentsRegister.setRegBy(ShiroUtils.getSysUser().getUserId().intValue());
        return toAjax(departAdminService.insertDepartmentsRegister(departmentsRegister));
    }


    public Integer getArryZero(String text) {
        String[] arry = text.split(",");
        return Integer.parseInt(arry[0]);
    }



    /**
     * 跳转历史页面
     */
    @GetMapping("/history")
    public String history(ModelMap mmap) {
        return prefix + "/historyDepartAdmin";
    }

    /**
     * 根据权限及条件查询部门登记表数据
     *
     * @param map
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map map) {
        Authority authority = authorityService.findAuthorAndIsAdmin();
        startPage();
        return getDataTable(departAdminService.findDepList(map, authority));
    }
    @GetMapping("/add")
    public String add() {
        return prefix + "/depAdd/addDepartAdmin";
    }
    @GetMapping("/add/register")
    public String add(ModelMap mmap) {
        List<Companys> companys = companysService.findAllCompanys(null);
        mmap.put("companys", companys);
        return prefix + "/depAdd/addRegister";
    }
    @GetMapping("/editAdd/{id}")
    public String editAdd(@PathVariable("id") String id,ModelMap mmap){
        List<Companys> companys = companysService.findAllCompanys(null);
        mmap.put("companys", companys);
        Map<String,Object> depMap = departAdminService.findDepAdd(id);
        mmap.put("dep",depMap);
        //根据所属公司找出下属部门
        if(depMap.get("CompId") != null){
            String compId = depMap.get("CompId").toString();
            mmap.put("xjDep",departAdminService.findDepByCompId(compId));
        }
        return prefix + "/depAdd/editRegister";
    }
    @RequestMapping("/editDepAdd")
    @ResponseBody
    public AjaxResult editDepAdd(@RequestParam Map map){
        return toAjax(departAdminService.editDepAdd(map));
    }
    @RequestMapping("/deleteDepAdd")
    @ResponseBody
    public AjaxResult deleteDepAdd(String ids){
        return toAjax(departAdminService.deleteDepAdd(ids));
    }
    @PostMapping("/procedureDepAdd")
    @ResponseBody
    public BaseResult procedureDepAdd(String allId, String procedure) {
        return departAdminService.procedureDepAdd(allId, procedure);
    }
    @GetMapping("/getPerson")
    public String getPerson(String type, ModelMap mmap) {
        mmap.put("type", type);
        return prefix + "/getPerson";
    }


    /**
     * 跳转变更页面
     */
    @GetMapping("/change")
    public String change(ModelMap mmap) {
        return prefix + "/depEdit/changeDepartAdmin";
    }
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id,ModelMap mmap){
        List<Companys> companys = companysService.findAllCompanys(null);
        mmap.put("companys", companys);
        Map<String,Object> depMap = departAdminService.findDepAdd(id);
        mmap.put("dep",depMap);
        //根据所属公司找出下属部门
        if(depMap.get("CompId") != null){
            String compId = depMap.get("CompId").toString();
            mmap.put("xjDep",departAdminService.findDepByCompId(compId));
        }
        return prefix + "/depEdit/edit";
    }
    @GetMapping("/addEditDep")
    public String addEditDep() {
        return prefix + "/depEdit/addEditDep";
    }
    @RequestMapping("/findEditDep")
    @ResponseBody
    public TableDataInfo findEditDep(@RequestParam Map map){
        Authority authority = authorityService.findAuthorAndIsAdmin();
        startPage();
        return getDataTable(departAdminService.findEditDep(map,authority));
    }
    /**
     * 执行添加员工存储过程
     */
    @PostMapping("/addEditDep")
    @ResponseBody
    public BaseResult addEditDep(String allDep,String type) {
        return departAdminService.addEditDep(allDep,type);
    }


    /**
     * 跳转失效页面
     */
    @GetMapping("/invalid")
    public String invalid(ModelMap mmap) {
        return prefix + "/depInvalid/invalidDepartAdmin";
    }
    @GetMapping("/addInvalidDep")
    public String addInvalidDep() {
        return prefix + "/depInvalid/addInvalidDep";
    }
    @GetMapping("/editInvalid/{id}")
    public String editInvalid(@PathVariable("id") String id,ModelMap mmap){
        Map<String,Object> depMap = departAdminService.findDepAdd(id);
        mmap.put("dep",depMap);
        return prefix + "/depInvalid/edit";
    }
    @RequestMapping("/editDepInvalid")
    @ResponseBody
    public AjaxResult editDepInvalid(@RequestParam Map map){
        return toAjax(departAdminService.editDepInvalid(map));
    }

    @RequestMapping("/findDataHistory")
    @ResponseBody
    public TableDataInfo findDataHistory(@RequestParam Map map){
        Authority authority = authorityService.findAuthorAndIsAdmin();
        return getDataTable(departAdminService.findDataHistory(map,authority));
    }


}
